package apresentacao.caminhoneiro;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import tcc.caminhoneiro.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class Fragment_Ajustes_Notificacoes extends Fragment{
	Spinner tempoSpin, raioSpin;
	boolean toastRaio, toastTempo;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_ajustes_notificacoes, null);
		inicializar(view);
		return view;
	}
	
	private void inicializar(View view){
		raioSpin = (Spinner) view.findViewById(R.id.RaioSpin);
		initRaioSpinner();
		OnItemSelectedListener eventoRaio = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Integer novoRaio = 10000;
				switch(position){
				  case 1:{
					  novoRaio = 25000;
					  break;
				  }
				  case 2:{
					  novoRaio = 50000;
					  break;
				  }
				  case 3:{
					  novoRaio = 75000;
					  break;
				  }
				  case 4:{
					  novoRaio = 100000;
					  break;
				  }
				}
				if (toastRaio){
				 write("notificationRaio.txt", "raio="+novoRaio+"="+position);
				 Toast.makeText(getActivity(), "Raio modificado", Toast.LENGTH_SHORT).show();
				}else toastRaio = true;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		raioSpin.setOnItemSelectedListener(eventoRaio);
		
		tempoSpin = (Spinner) view.findViewById(R.id.tempoSpin);
		initTempoSpinner();
		
		OnItemSelectedListener eventoTempo = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				int novoTempo = 10;
				switch(position){
				 case 1:{
					 novoTempo = 30;
					 break;
				 }
				 case 2:{
					 novoTempo = 60;
					 break;
				 }
				 case 3:{
					 novoTempo = 300;
					 break;
				 }
				 case 4:{
					 novoTempo = 600;
				 }
				}
				if (toastTempo){
				 write("notificationTime.txt", "time="+novoTempo+"="+position);
				 Toast.makeText(getActivity(), "Tempo modificado", Toast.LENGTH_SHORT).show();
				}else toastTempo = true;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		tempoSpin.setOnItemSelectedListener(eventoTempo);
	}
	
	private void initTempoSpinner(){
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.item_tempo,
				android.R.layout.simple_spinner_item);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    tempoSpin.setAdapter(adapter);
	    String str = readFile("notificationTime.txt");
	    int atual = 0;
	    if (str!=null) atual = Integer.valueOf(str.split("=")[2]);
	    toastTempo = false;
	    tempoSpin.setSelection(atual);
	}
	
	private void initRaioSpinner(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.item_raio,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    raioSpin.setAdapter(adapter);
	    String str = readFile("notificationRaio.txt");
	    int atual = 0;
	    if (str!=null) atual = Integer.valueOf(str.split("=")[2]);
	    toastRaio = false;
	    raioSpin.setSelection(atual);
	}
	
	private String readFile(String file){
		StringBuffer strBf = new StringBuffer("");
		String readString = "";
		try {
			FileInputStream fIn = getActivity().openFileInput ( file ) ;
            InputStreamReader isr = new InputStreamReader ( fIn ) ;
            BufferedReader buffreader = new BufferedReader ( isr ) ;
            readString = buffreader.readLine();
            strBf.append(readString);
            buffreader.close();
            isr.close ( ) ;
            fIn.close();
            return strBf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void write(String file, String content) {

		FileOutputStream fos;
		try {
			fos = getActivity().openFileOutput(file, Context.MODE_PRIVATE);
			fos.write(content.getBytes());
			fos.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
